﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Heyo;
using HeyoUi.DialogPages;
using HeyoUi.Views.DialogPages;

namespace HeyoUi.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : WindowPlus
    {
        const bool bgAnim = false;
        public MainWindow()
        {
            InitializeComponent();
            InitAnimation();
            GameSelectToggle.Checked += GameSelectToggle_Checked;
            GameSelectToggle.Unchecked += GameSelectToggle_Unchecked;
            OptionToggleBtn.Checked += OptionToggleBtn_Checked;
            OptionToggleBtn.Unchecked += OptionToggleBtn_Unchecked;

            Loaded += MainWindow_Loaded;
            SettingBtn.Click += SettingBtn_Click;
        }

        private void SecondPanelMatrix_CurrentTime(object sender, System.EventArgs e)
        {
            Rect rect = new Rect(0, 0,
                LeftPanel.RenderSize.Width + SecondPanel.RenderSize.Width +
                SecondPanelMatrix.RenderTransform.Value.OffsetX, LeftPanel.RenderSize.Height);
            RectangleGeometry geometry = new RectangleGeometry(rect);
            BgBlur.Clip = geometry;
        }

        private void SettingBtn_Click(object sender, RoutedEventArgs e)
        {


            if (GameSelectToggle.IsChecked == true)
            {
                GameSelectToggle.IsChecked = false;
            }
            SecondPanel.SelectedIndex = 0;
            if (_secondPanelShowed)
            {
                ShowSwitchAnimation();
            }
            else
            {
                ShowSecondPanel();
            }

        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //if(bgAnim)
            //{
            //    BgAnim.Start();
            //}
            //else
            //{
            //    BgColor.Background = new ImageBrush(new BitmapImage(new BackgroundImageUri("pack://application:,,,/Resources/bg.jpg")));
            //}
        }
        private bool _secondPanelShowed;


        private void OptionToggleBtn_Unchecked(object sender, RoutedEventArgs e)
        {
            if (GameSelectToggle.IsChecked == false)
                HideSecondPanel();
        }
        private void OptionToggleBtn_Checked(object sender, RoutedEventArgs e)
        {
            SecondPanel.SelectedIndex = 0; if (GameSelectToggle.IsChecked == true)
            {
                GameSelectToggle.IsChecked = false;
            }
            else if (_secondPanelShowed)
            {
                ShowSwitchAnimation();
            }
            else
            {
                ShowSecondPanel();
            }
        }

        private void GameSelectToggle_Unchecked(object sender, RoutedEventArgs e)
        {
            if (OptionToggleBtn.IsChecked == false)
                HideSecondPanel();
        }


        private void GameSelectToggle_Checked(object sender, RoutedEventArgs e)
        {
            SecondPanel.SelectedIndex = 1;
            if (OptionToggleBtn.IsChecked == true)
            {
                OptionToggleBtn.IsChecked = false;
                ShowSwitchAnimation();
            }
            else if (_secondPanelShowed)
            {
                ShowSwitchAnimation();
            }
            else
            {
                ShowSecondPanel();
            }
        }



        private void ShowSwitchAnimation()
        {
            SwitchOpacity.BeginAnimation();
        }
        private void ShowSecondPanel()
        {
            _secondPanelShowed = true;
            SecondPanelMatrix.BeginAnimation();
            SecondPanelOpacity.BeginAnimation();
            GameDetailMatrix.BeginAnimation();
            GameDetailOpacity.BeginAnimation();

            MainPageOpacityBorder.SpeedRatio = 10;
            MainPageMatrixBorder.BeginAnimation();
            MainPageOpacityBorder.BeginAnimation();
        }

        private void HideSecondPanel()
        {
            _secondPanelShowed = false;
            SecondPanelMatrix.BeginInverseAnimation();
            SecondPanelOpacity.BeginInverseAnimation();
            GameDetailMatrix.BeginInverseAnimation();
            GameDetailOpacity.BeginInverseAnimation();

            MainPageOpacityBorder.SpeedRatio = 3;
            MainPageMatrixBorder.BeginInverseAnimation();
            MainPageOpacityBorder.BeginInverseAnimation();
        }
        private void InitAnimation()
        {
            SwitchOpacity.OpacityFrom = 0;
            SwitchOpacity.OpacityTo = 1;
            SwitchOpacity.SpeedRatio = 3;
            SwitchOpacity.AnimFunction = new ExponentialEase() { Exponent = 4 };

            SecondPanelMatrix.TransFrom = new TranslateTransform(-50, 0);
            SecondPanelMatrix.TransTo = new TranslateTransform(0, 0);
            SecondPanelMatrix.AnimFunction = new ExponentialEase() { Exponent = 4 };
            SecondPanelMatrix.SpeedRatio = 3;
            SecondPanelOpacity.OpacityFrom = 0;
            SecondPanelOpacity.OpacityTo = 1;
            SecondPanelOpacity.SpeedRatio = 3;
            SecondPanelOpacity.AnimFunction = new ExponentialEase() { Exponent = 4 };

            GameDetailMatrix.TransFrom = new TranslateTransform(-60, 0);
            GameDetailMatrix.TransTo = new TranslateTransform(0, 0);
            GameDetailMatrix.AnimFunction = new ExponentialEase() { Exponent = 4 };
            GameDetailMatrix.SpeedRatio = 1.5;
            GameDetailOpacity.OpacityFrom = 0;
            GameDetailOpacity.OpacityTo = 1;
            GameDetailOpacity.SpeedRatio = 1.5;
            GameDetailOpacity.AnimFunction = new ExponentialEase() { Exponent = 4 };

            OptionToggleBtn.MouseEnter += (s, e) => { SettingButtonHint.BeginAnimation(); };
            OptionToggleBtn.MouseLeave += (s, e) => { SettingButtonHint.BeginInverseAnimation(); };
            GameSelectToggle.MouseEnter += (s, e) => { SelectGameButtonHint.BeginAnimation(); };
            GameSelectToggle.MouseLeave += (s, e) => { SelectGameButtonHint.BeginInverseAnimation(); };

            SecondPanelMatrix.Started += (s, e) =>
            {
                Grid.SetColumnSpan(BgBlur, 2);
                SecondPanelMatrix.Visibility = Visibility.Visible;
            };
            SecondPanelMatrix.InverseCompleted += (s, e) =>
            {
                Grid.SetColumnSpan(BgBlur, 1);
                SecondPanelMatrix.Visibility = Visibility.Collapsed;
            };
            SecondPanelMatrix.CurrentTime += SecondPanelMatrix_CurrentTime;
            GameDetailMatrix.Started += (s, e) => { GameDetailMatrix.Visibility = Visibility.Visible; };
            GameDetailMatrix.InverseCompleted += (s, e) => { GameDetailMatrix.Visibility = Visibility.Collapsed; };

            MainPageOpacityBorder.OpacityTo = 0;
            MainPageOpacityBorder.OpacityFrom = 1;
            MainPageOpacityBorder.SpeedRatio = 3;
            MainPageOpacityBorder.AnimFunction = new ExponentialEase() { Exponent = 4 };
            MainPageMatrixBorder.TransFrom = new TranslateTransform(0, 0);
            MainPageMatrixBorder.TransTo = new TranslateTransform(60, 0);
            MainPageMatrixBorder.AnimFunction = new ExponentialEase() { Exponent = 4 };
            MainPageMatrixBorder.SpeedRatio = 3;
        }



        private void Drag_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed) return;

            //LeftPanelColor.Opacity = 1;
            DragMove();

            //var da = new DoubleAnimation
            //{
            //    To = 0.7,
            //    Duration = new Duration(TimeSpan.FromMilliseconds(400)),
            //    FillBehavior = FillBehavior.Stop
            //};
            //da.Completed += (s, ee) => { LeftPanelColor.Opacity = 0.7; };
            //LeftPanelColor.BeginAnimation(OpacityProperty, da);
        }


        private void MouseAnimation_MouseDown(object sender, MouseButtonEventArgs e)
        {
            new DialogWindow(new TestDialogPage(), this).Show();
        }

    }
}
